package com.app.megaprosoft.modyimcarcare;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;

import com.megaprosoft.modyimcarcare.R;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class CheckActivity extends Activity {

	Button bth,btnB;
	TextView RegID,time;
	connect con = new connect(); 
	ResultSet rs;
	private boolean input=false;
	String text,id,TypeSer,RegisID,name,login,passin,DepCode,UserGroupCode,Time;
	ArrayList<String> Status = new ArrayList<String>();
	
	public static String DTIDS,ID;
	String StatuS,ServiceID,pass,NameInput,company,formattedDate,formattedDateShow,DTID;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		setContentView(R.layout.activity_check);
		
		NameInput = getIntent().getExtras().getString("name");
		company = getIntent().getExtras().getString("CompanyS");
		
		 bth = (Button) findViewById(R.id.button1);
		 btnB = (Button) findViewById(R.id.button2);
		 RegID = (TextView) findViewById(R.id.ShowRegID);
		// spnSts = (Spinner) findViewById(R.id.dropstatus);
		 
		 
		 Status();
		 statusByPersonnel();	
		 statusByHead();
		
		 
		 text = getIntent().getExtras().getString("DTID");
		 RegisID = getIntent().getExtras().getString("RegisterID");
		 TypeSer = getIntent().getExtras().getString("Type");
		 DTID = getIntent().getExtras().getString("TID");
		 
		 RegID.setText(RegisID);
		 
		 
		 ///////////////////////////time update ////////////////////
		 
		 Calendar c = Calendar.getInstance();
			SimpleDateFormat df = new SimpleDateFormat("HH:mm dd-MM-yyyy");
			SimpleDateFormat dfShow = new SimpleDateFormat("HH:mm");
			 formattedDate = df.format(c.getTime());
			 formattedDateShow = dfShow.format(c.getTime());

	        // txtResult
	        time = (TextView) findViewById(R.id.timer);
	        time.setText(formattedDate);
	        Time = time.getText().toString();
	        
////////////////////////////////  Button		
		bth.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				if(StatuS.equals("��ҧ��")){
				String sql = "UPDATE ServiceDetail_old SET Status = '"+StatuS+"',LastUpdate='"+Time+"',WetTime='"+formattedDateShow+
						     "' WHERE DTID = '"+DTID+"' " 	;

				if(con.editData(sql)){	 
				Toast.makeText(CheckActivity.this, "����¹�ŧʶҹ�����", Toast.LENGTH_SHORT).show();
				Intent newActivityAdd = new Intent(CheckActivity.this,Detail.class);
				newActivityAdd.putExtra("name", NameInput);
				newActivityAdd.putExtra("CompanyS", company);
				//newActivityAdd.putExtra("IntentID",text);
				startActivity(newActivityAdd);
				
				finish();
				}else{
			    Toast.makeText(CheckActivity.this, "ʶҹ��ѧ�������¹�ŧ", Toast.LENGTH_SHORT).show();
			    Intent newActivityAdd = new Intent(CheckActivity.this,Detail.class);
			    newActivityAdd.putExtra("name", NameInput);
				newActivityAdd.putExtra("CompanyS", company);
				startActivity(newActivityAdd);
				}
				}else if(StatuS.equals("������")){
				String sql = "UPDATE ServiceDetail_old SET Status = '"+StatuS+"',LastUpdate='"+Time+"',DryTime='"+formattedDateShow+
							 "' WHERE DTID = '"+DTID+"' " 	;

					if(con.editData(sql)){	 
					Toast.makeText(CheckActivity.this, "����¹�ŧʶҹ�����", Toast.LENGTH_SHORT).show();
					Intent newActivityAdd = new Intent(CheckActivity.this,Detail.class);
					newActivityAdd.putExtra("name", NameInput);
					newActivityAdd.putExtra("CompanyS", company);
					//newActivityAdd.putExtra("IntentID",text);
					startActivity(newActivityAdd);
					
					finish();
					}else{
				    Toast.makeText(CheckActivity.this, "ʶҹ��ѧ�������¹�ŧ", Toast.LENGTH_SHORT).show();
				    Intent newActivityAdd = new Intent(CheckActivity.this,Detail.class);
				    newActivityAdd.putExtra("name", NameInput);
					newActivityAdd.putExtra("CompanyS", company);
					startActivity(newActivityAdd);
					}
				
					
				}else{
					String sql = "UPDATE ServiceDetail_old SET Status = '"+StatuS+"',LastUpdate='"+Time+
							 "' WHERE DTID = '"+DTID+"' " 	;

					if(con.editData(sql)){	 
					Toast.makeText(CheckActivity.this, "����¹�ŧʶҹ�����", Toast.LENGTH_SHORT).show();
					Intent newActivityAdd = new Intent(CheckActivity.this,Detail.class);
					newActivityAdd.putExtra("name", NameInput);
					newActivityAdd.putExtra("CompanyS", company);
					//newActivityAdd.putExtra("IntentID",text);
					startActivity(newActivityAdd);
					
					finish();
					}else{
				    Toast.makeText(CheckActivity.this, "ʶҹ��ѧ�������¹�ŧ", Toast.LENGTH_SHORT).show();
				    Intent newActivityAdd = new Intent(CheckActivity.this,Detail.class);
				    newActivityAdd.putExtra("name", NameInput);
					newActivityAdd.putExtra("CompanyS", company);
					startActivity(newActivityAdd);
					}
				}
				 
			}
			});
		
		
		
		btnB.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
			 
				
				Intent Nintent = new Intent(CheckActivity.this, Detail.class);
				Nintent.putExtra("name", NameInput);
				Nintent.putExtra("CompanyS", company);
				startActivity(Nintent);
			}

			});
	
		
	}
	
	
/////////////////////Check status 1 //////////////////////////
	
private void Status() {
	
	String tusTmp = getIntent().getExtras().getString("Status");
	
	final Spinner spn = (Spinner) findViewById(R.id.dropstatus);

	Status.add(tusTmp);
	Status.add("����ԡ������");
	Status.add("��ҧ��");
	Status.add("������");
	Status.add("�͵�Ǩ�������º����");
		
	
	
ArrayAdapter<String> empolyeeAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,Status);
empolyeeAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
spn.setAdapter(empolyeeAdapter);

spn.setOnItemSelectedListener(new  AdapterView.OnItemSelectedListener() { 

public void onItemSelected(AdapterView<?> adapterView, 
	View view, int i, long l) { 
	// TODO Auto-generated method stub
	
	
	StatuS = Status.get(i);
	
	}
public void onNothingSelected(AdapterView<?> arg0) {
	// TODO Auto-generated method stub
	Toast.makeText(CheckActivity.this,
				String.valueOf("ʶҹ��ա������¹�ŧ����"),
					Toast.LENGTH_SHORT).show();
} 

});

}	

/////////////////////Check status 2 by Personnel//////////////////////////	
private	void statusByPersonnel(){
	final AlertDialog.Builder popDialog = new AlertDialog.Builder(this);
	final LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
	final Button BtnPnl = (Button) findViewById(R.id.buttonC1);
	
	BtnPnl.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
        	
            final View Viewlayout = inflater.inflate(R.layout.a_carcare_dialog_confirmuser,
                    (ViewGroup) findViewById(R.id.layout_dialog));                
			
            popDialog.setIcon(android.R.drawable.btn_star_big_on);	
    		popDialog.setTitle("��س������ͼ����������ʼ�ҹ ����Ѻ��ѡ�ҹ ");
            popDialog.setView(Viewlayout);
            
            // Button OK
            popDialog.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener(){

                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                    
                   // Username and Password (Dialog)
                    EditText user = (EditText) Viewlayout.findViewById(R.id.txtUsername);
             
                   
                    String userLogin = user.getText().toString();
                   
                    rs = con.selectData("SELECT * FROM FFUser WHERE LoginName='"+userLogin+"' and DepCode='101201301220004' ");
                  //  String resultServer  = getPost(sqll,params);
                    
                    try {
            			
                    	if(rs.next()){
                    	
            			login = rs.getString("LoginName");
            			passin = rs.getString("Password");
            			name = rs.getString("UserName");
            				
            			 if(userLogin.equals(login)||userLogin.equals("user")){
                             
                             Intent newActivity = new Intent(CheckActivity.this,CheckCarByPnlActivity.class);
                             input = true;
                             newActivity.putExtra("IntentID", text);
                             newActivity.putExtra("RegisID", RegisID);
                             newActivity.putExtra("nameCheck", name);
                             newActivity.putExtra("Type", TypeSer);
                             newActivity.putExtra("name", NameInput);
                             newActivity.putExtra("CompanyS", company);
                             newActivity.putExtra("Status", StatuS);
                             startActivity(newActivity);
                           
                             Toast.makeText(CheckActivity.this,"���ʼ�ҹ�١��ͧ " ,
                    	 		     Toast.LENGTH_LONG).show(); 
                         }
 
            			
            		} 

                  	else { 
   				 		Toast.makeText(CheckActivity.this,"�Դ��Ҵ!!!!",
   		       	 		     Toast.LENGTH_LONG).show(); 
   				 	     } 
                    } 
                    catch (SQLException e1) {
            			// TODO Auto-generated catch block
            			e1.printStackTrace();
            		}
         
            		
                }
		                
                

            })
            
            // Button Cancel
            .setNegativeButton("Cancel",
					  new DialogInterface.OnClickListener() {
					    public void onClick(DialogInterface dialog,int id) {
						dialog.cancel();
					    }
					  });

            popDialog.create();
            popDialog.show(); 
        	
        }	
    });
	
	
}
	
//////////////////////////////////test//////////////////////////////////

//////////////////test///////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////

/////////////////////Check status 3 by Personnel Head//////////////////////////	
private	void statusByHead(){
	final AlertDialog.Builder popDialog = new AlertDialog.Builder(this);
	final LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
	final Button BtnPnl = (Button) findViewById(R.id.buttonC2);
	
	BtnPnl.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
        	
            final View Viewlayout = inflater.inflate(R.layout.a_carcare_dialog_confirmuser,
                    (ViewGroup) findViewById(R.id.layout_dialog));                
			
            popDialog.setIcon(android.R.drawable.btn_star_big_on);	
    		popDialog.setTitle("��س������ͼ����������ʼ�ҹ ����Ѻ���˹�ҧҹ ");
            popDialog.setView(Viewlayout);
            
            // Button OK
            popDialog.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener(){

                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                    
                    // txtUsername and Password (Dialog)
                    EditText user = (EditText) Viewlayout.findViewById(R.id.txtUsername);
                  //  EditText pass = (EditText) Viewlayout.findViewById(R.id.txtPassword);
                    
                    // txtResult (Main Screen)
                    //TextView result = (TextView) findViewById(R.id.txtResult);
                   // result.setText(" ���ͼ���� : " + user.getText().toString() + " \n" +
                   // 		" ���ʼ�ҹ : " + pass.getText().toString() + " ");
                   
                    String userLogin = user.getText().toString();
                   // String passLogin = pass.getText().toString();
                    
                    
                    rs = con.selectData("SELECT * FROM FFUser WHERE LoginName ='"+userLogin+"' and DepCode = '101201301220001' ");
                    //  String resultServer  = getPost(sqll,params);
                      
                      try {
              			
                      	if(rs.next()){
                      	
              			login = rs.getString("LoginName");
              			passin = rs.getString("Password");
              			name = rs.getString("UserName");
              				
              			 if(login.equals(userLogin)){
                               
                               Intent newActivity = new Intent(CheckActivity.this,CheckCarByHedActivity.class);
                               input = true;
                               newActivity.putExtra("IntentID", text);
                               newActivity.putExtra("RegisID", RegisID);
                               newActivity.putExtra("nameCheck", name);
                               newActivity.putExtra("Type", TypeSer);
                               newActivity.putExtra("name", NameInput);
                               newActivity.putExtra("CompanyS", company);
                               newActivity.putExtra("Status", StatuS);
                               startActivity(newActivity);
                             
                               Toast.makeText(CheckActivity.this,"���ʼ�ҹ�١��ͧ " ,
                      	 		     Toast.LENGTH_LONG).show(); 
                           }
                                  				 	
                      	}
              			
                      	else { 
       				 		Toast.makeText(CheckActivity.this,"�Դ��Ҵ!!!!",
       		       	 		     Toast.LENGTH_LONG).show(); 
       				 	}   	
                      	
                      	
              		} catch (SQLException e1) {
              			// TODO Auto-generated catch block
              			e1.printStackTrace();
              		}
           
              		
                  }
  		                
                  

              })
            
            // Button Cancel
            .setNegativeButton("Cancel",
					  new DialogInterface.OnClickListener() {
					    public void onClick(DialogInterface dialog,int id) {
						dialog.cancel();
					    }
					  });

            popDialog.create();
            popDialog.show(); 
        	
        }	
    });
	
	
}

@Override
public void onBackPressed() {
  
        new AlertDialog.Builder(this)
        .setTitle("¡��ԡ��÷���¡��")
        .setMessage("��Ѻ�˹����ѡ")
                .setNegativeButton(android.R.string.yes, new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface arg0, int arg1) {
                        Intent newActivity = new Intent(getApplicationContext(),MainActivity.class);	                          
                        newActivity.putExtra("name", NameInput);
        				newActivity.putExtra("CompanyS", company);
                        startActivity(newActivity);
                        finish();
                        overridePendingTransition(R.layout.fadeendout,R.layout.fadeendin);
                    }
                })
                .setPositiveButton(android.R.string.no, null).create().show();
    

}


	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
